#include "pagemanager.h"

PageManager::PageManager(QWidget *parent) : QTabWidget(parent)
{
//    PageWidget* blankpage1 = new PageWidget(this);
//    PageWidget* blankpage2 = new PageWidget(this);
//    m_PageWidgetList.append(blankpage1);
//    m_PageWidgetList.append(blankpage2);
//    addTab(blankpage1, "Page1");
//    addTab(blankpage2, "Page2");
}

void PageManager::split(Qt::Orientation orientation)
{
    m_PageWidgetList.at(currentIndex())->split(orientation);
}

void PageManager::split_hor()
{
    split(Qt::Horizontal);
}

void PageManager::split_ver()
{
    split(Qt::Vertical);
}

void PageManager::saveSettings(QSettings* qs)
{
    qs->beginGroup("PageManager");
    qs->remove("");//remove all old keys in current group.
    //add new keys to group.
    foreach (PageWidget *page, m_PageWidgetList) {
        qs->beginGroup(page->name());
        page->saveSettings(qs);
        qs->endGroup();
    }
    qs->endGroup();
}

void PageManager::readSettings(QSettings* qs)
{
    if(!qs->childGroups().contains("PageManager")){
        PageWidget* blankpage1 = new PageWidget(this, "Page1");
        PageWidget* blankpage2 = new PageWidget(this, "Page2");
        PageWidget* blankpage3 = new PageWidget(this, "Page3");
        m_PageWidgetList.append(blankpage1);
        m_PageWidgetList.append(blankpage2);
        m_PageWidgetList.append(blankpage3);
        addTab(blankpage1, blankpage1->name());
        addTab(blankpage2, blankpage2->name());
        addTab(blankpage3, blankpage3->name());
        return;
    }
    qs->beginGroup("PageManager");
    foreach (QString pagename, qs->childGroups()) {

        PageWidget* blankpage = new PageWidget(this, pagename);
        m_PageWidgetList.append(blankpage);
        addTab(blankpage, blankpage->name());
        qs->beginGroup(pagename);
        blankpage->readSettings(qs);
        qs->endGroup();
    }
    qs->endGroup();
}
